package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private DepartmentService departmentService;

    @Override
    public void saveDepartment(Map<String, Object> depParams) {
//        将map集合转化为POJO类对象
        String str = JSONObject.toJSONString(depParams);
        Department department = JSONObject.parseObject(str, Department.class);

//        医院编号+科室编号 进行联合查询
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();
//        首字母大写
        Department platformDep = departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);

        if (platformDep == null){  //如果科室中没有信息,则进行添加操作
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else{ // 如果存在科室,则进行修改操作

//            修改比添加多了一个Id的字段,其他的都是一样的
            department.setCreateTime(platformDep.getCreateTime());
            department.setUpdateTime(new Date());
            department.setId(platformDep.getId());
            department.setIsDeleted(platformDep.getIsDeleted());
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> getDepartmentPage(Map<String, Object> depParams) {
        int pageNum = Integer.parseInt((String) depParams.get("page"));
        int limit = Integer.parseInt((String) depParams.get("limit"));
        String hoscode = (String) depParams.get("hoscode");

//        查询条件
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> ex = Example.of(department);

//        分页
        Pageable pageable = PageRequest.of(pageNum-1,limit);
        return departmentRepository.findAll(ex, pageable);
    }

//    删除思路:  1.通过医院的编号和科室的编号确定科室的对象
//              2.通过科室对象获取科室的Id的信息 然后通过Id进行删除
    @Override
    public void remove(Map<String, Object> depParams) {
//        获取医院的编号和科室的编号
        String hoscode = (String) depParams.get("hoscode");
        String depcode = (String) depParams.get("depcode");

        Department depaetment = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (depaetment != null){
            departmentRepository.deleteById(depaetment.getId());
        }
    }

//    获取科室排班信息
    @Override
    public List<DepartmentVo> getDepartmentList(String hoscoded) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();

        //根据医院编号，查询医院所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscoded);
        Example example = Example.of(departmentQuery);
        //所有科室列表 departmentList
        List<Department> departmentList = departmentRepository.findAll(example);

        //根据大科室编号  bigcode 分组，获取每个大科室里面下级子科室
        Map<String, List<Department>> deparmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合 deparmentMap
        for(Map.Entry<String,List<Department>> entry : deparmentMap.entrySet()) {
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> deparment1List = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(deparment1List.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for(Department department: deparment1List) {
                DepartmentVo departmentVo2 =  new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                //封装到list集合
                children.add(departmentVo2);
            }
            //把小科室list集合放到大科室children里面
            departmentVo1.setChildren(children);
            //放到最终result里面
            result.add(departmentVo1);
        }
        //返回
        return result;
    }

//    根据医院编号和科室编号获取科室的名字
    @Override
    public String getDepName(String hoscode, String depcode) {
        String result="";
        Department departmentObj = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (departmentObj!=null){
            result = departmentObj.getDepname();
        }else{
            throw new YyghException(20001,"获取排班信息失败");
        }
        return result;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);
    }


}
